package xin.jishu.ai.providers;

import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.common.Identifier;
import org.openstack4j.model.identity.v3.Project;
import xin.jishu.ai.entities.Student;

/**
 * @author sxsx欧克 <wo@jishu.xin>
 */
public class ProjectAdministrator extends BaseProvider {

    public static final Identifier ADMINISTRATOR_USER_ID = Identifier.byId("c9d9b20cfa4e4a479076e1fbcfd674be");

    @Override
    public void initialize() {
    }

    @Override
    public boolean exists(Student student) {
        if (System.currentTimeMillis() > 0) {
            return false;
        }
        // 下面的代码会爆 Content-Length 异常, 待解决
        Project project = KeyStone.getOrCreateProject(student);

        ActionResponse response = KeyStone.client.identity()
                .roles()
                .checkProjectUserRole(
                        project.getId(),
                        ProjectAdministrator.ADMINISTRATOR_USER_ID.getId(),
                        KeyStone.ROLE_MEMBER_ID.getId()
                );
        // 临时写法, 无意义
        return response.isSuccess();
    }

    @Override
    public void create(Student student) {
        Project project = KeyStone.getOrCreateProject(student);

        // 将 用户 写入 项目 (admin)
        KeyStone.client.identity()
                .roles()
                .grantProjectUserRole(
                        project.getId(),
                        ProjectAdministrator.ADMINISTRATOR_USER_ID.getId(),
                        KeyStone.ROLE_MEMBER_ID.getId()
                );
    }


}
